/**
 * PackModelServiceImpl.java
 * Created at 2017-4-18
 * Created by shihui
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.RmsUtil;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.basic.model.TrPackCustomFactory;
import com.broadtext.rms.basic.model.TtPackModel;
import com.broadtext.rms.basic.service.IPackModelService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: PackModelServiceImpl</p>
 * <p>Description: 电池型号服务层</p>
 * <p>Author: shihui</p>
 * <p>Date: 2017-4-18</p>
 */
@Service("packModelServiceImpl")
public class PackModelServiceImpl extends EntityServiceImpl<TtPackModel> implements IPackModelService {

    /**
     * 
     * <p>Description: 注解入相关DAO</p>
     * @param entityDao 实体dao
     */
    @Autowired
    @Qualifier("ttPackModelDao")
    public void setEntityDao(EntityDao<TtPackModel> entityDao) {
        super.entityDao = entityDao;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public EntityPageBean<TtPackModel> getPackModels(Map<String, Object> map, PageBounds pageBounds) throws ServiceException {
        
        EntityPageBean pb;
        pb = new EntityPageBean();
        try {
            pb = this.findEntityPageByQuery(map, pageBounds);
        } catch (ServiceException e) {
            throw new ServiceException(e);
        }
        return pb;
    }

    @Override
    public boolean deleteByIds(String[] idArray, String sysUser) throws ServiceException {
        if (this.checkModelEditable(idArray)) {
            for (String id : idArray) {
                TtPackModel model;
                model = new TtPackModel();
                model = this.findEntityById(id);
                if (model != null) {
                    model.setDelFlag("1");
                    model.setUpdateUser(sysUser);
                    model.setUpdateDate(new Date());
                    super.updateRecord(model);
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public TtPackModel findPackModelById(String pkId) throws ServiceException {
        TtPackModel model;
        try {
            model = (TtPackModel) this.entityDao.findById(
                "com.broadtext.rms.basic.model.TtPackModel.findPackModelDetailById", pkId);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return model;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object findPackModelList() throws ServiceException {
        List<Object> list;
        list = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TtPackModel.findPackModelOpt", null);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return list;
    }

    @Override
    public boolean checkModelExist(TtPackModel model) throws ServiceException {
        List<TtPackModel> list;
        try {
            list = this.entityDao.findEntityByProperty(
                "com.broadtext.rms.basic.model.TtPackModel.findExistEntity", model);
            if (list.size() != 0) {
                //电池型号已存在
                return true;
            }
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return false;
    }

    @Override
    public boolean checkModelEditable(String[] ids) throws ServiceException {
        long countUsed;
        try {
            countUsed = (long) this.entityDao.findByProperty(
                "com.broadtext.rms.basic.model.TtPackModel.getPackModelUsedCount", ids).get(0);
            if (countUsed == 0) {
                return true;
            }
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return false;
    }

    /** (non-Javadoc)
     * <p>Title: initBatteryCorrelation</p>
     * <p>Description: </p>
     * @param id
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.basic.service.IPackModelService#initBatteryCorrelation(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> initBatteryCorrelation(String id) throws ServiceException {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> packModel = new HashMap<String, Object>();
        try{
            packModel = (Map<String, Object>)this.entityDao.findById("com.broadtext.rms.basic.model.TtPackModel.findBatteryCorrelationEntity", id);
            if (!RmsUtil.isBlank(packModel.get("pkId"))) {
                Map<String, Object> queryMap = new HashMap<String, Object>();
                queryMap.put("packModel", packModel.get("pkId"));
                map.put("packModel", packModel);
                map.put("multiSelect", this.entityDao.findByQuery("com.broadtext.rms.basic.model.TtPackModel.findBatteryCorrelationMutileSelect", queryMap));
            }
        } catch(DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return map;
    }

    /** (non-Javadoc)
     * <p>Title: saveBatteryCorrelation</p>
     * <p>Description: 保存电池型号关联信息</p>
     * @param ttPackModel
     * @param loginUser
     * @throws ServiceException
     * @see com.broadtext.rms.basic.service.IPackModelService#saveBatteryCorrelation(com.broadtext.rms.basic.model.TtPackModel, java.lang.String)
     */
    @Override
    public void saveBatteryCorrelation(TtPackModel ttPackModel, String loginUser) throws ServiceException {
        String[] idArray;
        idArray = new String[]{};
        String packCodes;
        packCodes = ttPackModel.getPackCode();
        if (!"".equals(packCodes)) {
            idArray = packCodes.split(",");
        }
        String packModel = ttPackModel.getPkId();
        try {
            this.entityDao.updateRecord("com.broadtext.rms.basic.model.TtPackModel.updatePackModel", packModel);
            for (String packCode : idArray) {
                Map<String, Object> paramMap = new HashMap<String, Object>();
                paramMap.put("packModel", packModel);
                paramMap.put("packCode", packCode);
                this.entityDao.updateRecord("com.broadtext.rms.basic.model.TtPackModel.updatePackModelByPackCode", paramMap);
            }
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }
    
    
}
